package shuishijie.suishijie.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.text.TextUtils;


import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;

import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.io.ByteArrayOutputStream;

import shuishijie.suishijie.R;
import shuishijie.suishijie.model.EnvConfig;
import shuishijie.suishijie.model.WechartPay;

/**
 * Created by mindx on 2017/9/7.
 */

public class WechartUtil {
    //    private static String APPID = "wx4a441cc9b654918b";
//    private static String APPID = "wxfa7c3f991601f2fe";
//    public static String APPID = "wx85d0d7c5c6b1b68a";
    public static String APPID = "wx6bebbbcb29849b0f";
    private static int imgSize = 120;
    private IWXAPI api;
    private static WechartUtil instance;

    public static WechartUtil getInstance() {
        if (instance == null) {
            instance = new WechartUtil();
        }
        return instance;
    }

    public void regTowWx(Context context) {
        api = WXAPIFactory.createWXAPI(context, APPID, true);
        api.registerApp(APPID);
    }

    boolean send = false;

    public void shareHtml(Context context, String title, String msg, String url, String imgPath, boolean sceneTimeline) {
        WXWebpageObject webpageObject = new WXWebpageObject();
        webpageObject.webpageUrl = url;
        if (TextUtils.isEmpty(webpageObject.webpageUrl)) {
           // Logger.e("分享链接有问题，启用主页分享");
            webpageObject.webpageUrl = EnvConfig.getWeb();
        }

        WXMediaMessage mediaMessage = new WXMediaMessage(webpageObject);
        mediaMessage.title = title;
        mediaMessage.description = msg;

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction(null);
        req.message = mediaMessage;
        req.scene = sceneTimeline ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession;

        send = false;
//        Glide.with(context)
//                .asBitmap()
//                .load(imgPath)
//                .into(new SimpleTarget<Bitmap>() {
//                    @Override
//                    public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
//                        if (resource != null) {
//                            mediaMessage.thumbData = bmpToByteArray(resource, false);
//                        }
//                        if (!send) {
//                            send = true;
//                            api.sendReq(req);
//                        }
//                    }
//
//                    @Override
//                    public void onLoadFailed(@Nullable Drawable errorDrawable) {
//                        super.onLoadFailed(errorDrawable);
//                        mediaMessage.thumbData = bmpToByteArray(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher), true);
//                        if (!send) { // glide会多次进入fail，防止画面死循环
//                            send = true;
//                            api.sendReq(req);
//                        }
//                    }
//                });
    }

    private String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }

    public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        if (!bmp.isRecycled()) {
            bmp.compress(Bitmap.CompressFormat.PNG, 90, output);
        }
        byte[] result = output.toByteArray();
        try {
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (needRecycle) {
            bmp.recycle();
        }
        return result;
    }

    public void pay(WechartPay req) {

        PayReq request = new PayReq();
        request.appId = req.appId;
        request.partnerId = req.partnerId;
        request.prepayId = req.prepay_id;
        request.packageValue = req.pkg;
        request.nonceStr = req.nonceStr;
        request.timeStamp = req.timeStamp;
        request.sign = req.sign;
        api.sendReq(request);
    }
}
